package com.zrwl.poscloud.core.task.order;

import com.zrwl.poscloud.base.entity.UserServiceOrderDetail;
import com.zrwl.poscloud.base.entity.UserServiceOrderUse;
import com.zrwl.poscloud.xo.mapper.UserServiceOrderDetailMapper;
import com.zrwl.poscloud.xo.mapper.UserServiceOrderUseMapper;
import com.zrwl.poscloud.xo.service.UserServiceOrderDetailService;
import com.zrwl.poscloud.xo.service.UserServiceOrderUseService;
import com.zrwl.poscloud.xo.vo.UserServiceOrderDetailVO;
import com.zrwl.poscloud.xo.vo.UserServiceOrderUseVO;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@ApiModel(value = "定时任务")
@Slf4j
public class DailyDeduction {

    @Resource
    private UserServiceOrderUseMapper userServiceOrderUseMapper;
    @Resource
    private UserServiceOrderDetailMapper userServiceOrderDetailMapper;

    @Autowired
    private UserServiceOrderUseService userServiceOrderUseService;

    @Autowired
    private UserServiceOrderDetailService userServiceOrderDetailService;


    /**
     * 1:获取未消费完的订单明细
     * 2:遍历订单明细进行日扣费处理
     * 2.1:获取最近一条使用记录（多个商品类型）
     * 2.2:若不存在则使用明细的购买量进行扣除日费
     * 2.3:若存在则用当前使用记录的使用前数量减去使用数量得到可扣除数量进行扣除
     */
    @Scheduled(cron = "0 0 0 1 * ?")
//    @Scheduled(fixedDelay = 10000)
    public void dayFee() {
        List<UserServiceOrderDetailVO> list = userServiceOrderDetailMapper.getUserServiceOrderDetaillist();
        List<UserServiceOrderUseVO> voList = userServiceOrderUseMapper.getUserServiceOrderUseVO();
        if (list.size() > 0) {
            if (voList.size() == 0) {
                for (UserServiceOrderDetailVO userServiceOrderDetailVO : list) {
                    UserServiceOrderUseVO userServiceOrderUseVO = new UserServiceOrderUseVO();
                    userServiceOrderUseVO.setUid(null);
                    userServiceOrderUseVO.setOrderUid(userServiceOrderDetailVO.getOrderUid());
                    userServiceOrderUseVO.setCellUid(userServiceOrderDetailVO.getCellUid());
                    userServiceOrderUseVO.setBeforeCount(userServiceOrderDetailVO.getCount());
                    userServiceOrderUseVO.setUseCount(1);
                    userServiceOrderUseVO.setReason("日扣费");
                    userServiceOrderUseService.add(userServiceOrderUseVO);
                    userServiceOrderDetailVO.setUsedCount(userServiceOrderDetailVO.getUsedCount()+1);
                    userServiceOrderDetailService.edit(userServiceOrderDetailVO);
                }
            } else {
                for (UserServiceOrderUseVO serviceOrderUseVO : voList) {
                    UserServiceOrderUseVO orderUseVO = new UserServiceOrderUseVO();
                    orderUseVO.setOrderUid(serviceOrderUseVO.getOrderUid());
                    orderUseVO.setCellUid(serviceOrderUseVO.getCellUid());
                    orderUseVO.setBeforeCount(serviceOrderUseVO.getBeforeCount() - serviceOrderUseVO.getUseCount());
                    orderUseVO.setUseCount(1);
                    orderUseVO.setReason("日扣费");
                    userServiceOrderUseService.add(orderUseVO);
                    List<UserServiceOrderDetailVO> listByOrderUidAndProType = userServiceOrderDetailMapper.getListByOrderUidAndProType(serviceOrderUseVO.getOrderUid(), serviceOrderUseVO.getCellUid());
                    for (UserServiceOrderDetailVO userServiceOrderDetailVO : listByOrderUidAndProType) {
                        userServiceOrderDetailVO.setUsedCount(userServiceOrderDetailVO.getUsedCount() + 1);
                        userServiceOrderDetailService.edit(userServiceOrderDetailVO);
                    }
                }
            }
        }
    }
}
